package com.yth.extension;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;

/**
 * bean的销毁
 * <ol>
 *     <li>DestructionAwareBeanPostProcessor::requiresDestruction          </li>
 *     <li>DestructionAwareBeanPostProcessor::postProcessBeforeDestruction </li>
 *     <li>PreDestroy::preDestroy                                          </li>
 *     <li>DisposableBean::destroy                                         </li>
 * </ol>
 *
 * @author yutianhong
 * @version 1.0
 * @since 2024/11/14 11:20
 */
@Slf4j
public class TestDestructionAwareBeanPostProcessor implements DestructionAwareBeanPostProcessor {

    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
        // 在一个bean销毁前，在一个bean执行PreDestroy之前
        log.info("[DestructionAwareBeanPostProcessor::postProcessBeforeDestruction] \t>>> bean: {}, beanName: {}", bean, beanName);
    }

    @Override
    public boolean requiresDestruction(Object bean) {
        // 在一个bean执行postProcessBeforeDestruction之前，紧跟在执行postProcessAfterInitialization之后
        log.info("[DestructionAwareBeanPostProcessor::requiresDestruction] \t>>> bean: {}", bean);
        return DestructionAwareBeanPostProcessor.super.requiresDestruction(bean);
    }
}
